package com.example.ViewPagerSum.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.ViewPagerSum.PhotoDetailActivity;
import com.example.ViewPagerSum.R;
import com.example.ViewPagerSum.adapter.WaterfallAdapter;
import com.example.ViewPagerSum.domain.MmPhoto;
import com.example.ViewPagerSum.domain.ResultTask;
import com.example.ViewPagerSum.interfaces.FinishedCallBack;
import com.example.ViewPagerSum.task.CarTask;
import com.example.ViewPagerSum.util.Constants;
import com.example.ViewPagerSum.util.ParseMmJson;
import main.java.com.huewu.pla.lib.MultiColumnPullToRefreshListView;
import main.java.com.huewu.pla.lib.internal.PLA_AdapterView;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by wangjing on 2015/9/9at0:25.
 * <p/>
 * Email:wjontheway@163.com
 * <p/>
 * ViewPagerSum   清纯
 */
public class SubCarMmFragment extends Fragment implements MultiColumnPullToRefreshListView.OnRefreshListener, FinishedCallBack {
    private MultiColumnPullToRefreshListView waterfallView;//可以把它当成一个listView
    private WaterfallAdapter adapter;
    private  Activity mActivity;
    private ArrayList<MmPhoto> mmPhotoList;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View  view=null;
        view=inflater.inflate(R.layout.sub_fragment_hot,container,false);
        waterfallView = (MultiColumnPullToRefreshListView) view.findViewById(R.id.list);
        waterfallView.setOnRefreshListener(this);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
       CarTask  task = new CarTask(this);
        task.execute();
    }


    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        CarTask  task = new CarTask(this);
        task.execute();
        Toast.makeText(mActivity,"我刷！",Toast.LENGTH_SHORT).show();
        // TODO Auto-generated method stub
        //下拉刷新要做的事
        //刷新完成后记得调用这个
        waterfallView.onRefreshComplete();
    }
    @Override
    public void onFinishedCallBack(ResultTask resultTask) {
        if (resultTask != null) {
            if(resultTask.action== Constants.ACTION_CAR_TASK){
                setupCar((JSONObject)resultTask.data);
            }

        }
    }
    private void setupCar(JSONObject data) {
        if (data != null) {
            ParseMmJson mParseMmJson=new ParseMmJson();
            mmPhotoList = mParseMmJson.getMmPhotoList(data);
            adapter = new WaterfallAdapter(mmPhotoList,mActivity);
            waterfallView.setAdapter(adapter);
        }else{
            //TODO 提示用户当前没有网络
        }
    }

}
